﻿using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Controls;
using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.FrontEndComponents;
using SharpDepend.Datatypes;
using SharpDepend.Utils;
using SoundEngine3.DspPlugins.Meta;
using System.Collections.Generic;
using System.Text;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.Container;

namespace DefaultDspPlugins.UI.Common.Modulizer.Engine.GUI.Modules
{
    public class DefaultModule : BaseModuleUI
    {
        public DefaultModule(BaseModule module) : base(module)
        {
        }
        
        protected override string OnSetLayout(BaseModule module)
        {
            StringBuilder dialsBuilder = new StringBuilder();

            // Create dials for all parameters.
            IList<ParameterProperties> parameters = module.Parameters;
            if (module is GroupModule)
            {
                GroupModule groupModule = module as GroupModule;
                parameters = groupModule.GetVisibleParameters();
            }

            if (parameters.Count > 0)
            {
                // Calculate required of columns and rows.
                int columns;
                int rows;
                Math.GetOptimizedGrid(parameters.Count, out rows, out columns);
                double sx = 100.0 / columns;
                double sy = 90.0 / rows;

                for (int i = 0; i < parameters.Count; i++)
                {
                    var parameter = parameters[i];

                    double px = 100.0 / columns * (i % columns);
                    double py = 90.0 / rows * (i / columns) + 10;

                    string dialDescription = @"
        [Dial]
		{
			Name = LabelName;
			[Properties]
			{
				Text = {Name};
				Text color = 255 255 255 255;
				Text size = 16;
				Text auto = True;
				Dimension procent = True;
				MinSize = 0 0;
				MaxSize = 1E+10 1E+10;
				Bg Color = 0 0 0 0;
				Texture = NULL;
				Overlay texture = NULL;
				Overlay texture color = 255 255 255 255;
				Name = LabelName;
				TagName = ;
				Position = {px} {py} 0;
				Rotation = 0 0 0;
				Scale = {sx} {sy} 0;
				Visible = True;
                DspParameter = {i};
			}
		}
";
                    dialDescription = dialDescription.Replace("{px}", px.ToString());
                    dialDescription = dialDescription.Replace("{py}", py.ToString());
                    dialDescription = dialDescription.Replace("{sy}", sy.ToString());
                    dialDescription = dialDescription.Replace("{sx}", sx.ToString());
                    dialDescription = dialDescription.Replace("{Name}", parameter.Label + "_" + i.ToStringFast());
                    dialDescription = dialDescription.Replace("{i}", i.ToStringFast());

                    dialsBuilder.AppendLine(dialDescription);
                }
            }

            string defaultUI =
@"
[ModuleUIPanel]
{
	Name = ModuleUIPanel333;
	[Properties]
	{
		ExpR = 0.8;
		ExpG = 0.8;
		ExpB = 0.8;
		Dimension procent = True;
		MinSize = 0 0;
		MaxSize = 10000000000 10000000000;
		Bg Color = 255 255 255 255;
		Overlay texture = NULL;
		Overlay texture color = 255 255 255 255;
		Name = ModuleUIPanel333;
		TagName = ;
		Position = 0 0 0;
		Rotation = 0 0 0;
		Scale = 100 100 0;
		Visible = True;
	}
    [Components]
    {
        [Label]
		{
			Name = LabelName;
			[Properties]
			{
				Text = {Name};
				Text color = 255 255 255 255;
				Text size = 16;
				Text auto = True;
				Dimension procent = True;
				MinSize = 0 0;
				MaxSize = 1E+10 1E+10;
				Bg Color = 0 0 0 0;
				Texture = NULL;
				Overlay texture = NULL;
				Overlay texture color = 255 255 255 255;
				Name = LabelName;
				TagName = ;
				Position = 0 0 0;
				Rotation = 0 0 0;
				Scale = 100 10 0;
				Visible = True;
			}
		}
{Dials}
    }
}
".Replace("{Dials}", dialsBuilder.ToString()).Replace("{Name}", module.Name);

            return defaultUI;
        }

        private List<BaseModulizerControl> controls;

        protected override void OnUICreated(ModuleUIPanel uiPanel)
        {
            this.controls = uiPanel.FindAll<BaseModulizerControl>();
        }

        public override void Refresh()
        {
            foreach (BaseModulizerControl control in this.controls)
            {
                if (control.CurrentParameter != null)
                {
                    control.BackgroundColor = Color.Transparent;
                    bool flag = (control.CurrentParameter as ModuleParameter).Module.IsTopModule();
                    bool module = (control.CurrentParameter as ModuleParameter).Module._IsRenderInTrackArea;
                    if (!(control.CurrentParameter as ModuleParameter).InternallyVisible)
                    {
                        if (!flag)
                        {
                            control.Visible = true;
                        }
                        else
                        {
                            control.Visible = true;
                        }
                    }
                    else if (!flag || module)
                    {
                        control.BackgroundColor = Color.Red;
                        control.Visible = true;
                    }
                    else
                    {
                        control.Visible = false;
                    }
                }
                else
                {
                    control.BackgroundColor = Color.Pink;
                }
            }
        }

        protected override void OnUIDestoyed(ModuleUIPanel uiPanel)
        {
            this.controls.Clear();
            this.controls = null;
        }
    }
}
